package com.hmall.item.Taa;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.item.domain.po.Item;
import com.hmall.item.domain.po.ItemDoc;
import com.hmall.item.service.IItemService;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@SpringBootTest
public class Taa {
    @Autowired
    private IItemService itemService;
    private RestHighLevelClient client;

    @BeforeEach
    public void init() {
        this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.12.168:9200")));
    }

    @AfterEach
    public void close() throws IOException {
        if (this.client != null) {
            this.client.close();
        }
    }
    @Test
    public void test() {
        System.out.println("hello world");
       int total=476;
       int pageCount=total/1000;
       if(total%1000!=0){
           pageCount++;
       }
       System.out.println(pageCount);
    }
    //批量导入item数据到到es
    @Test
    public void test2() throws IOException {
      //分页获取
        int pageNum=1;
        int pageSize=1000;
        while (true){
            System.out.println("----------开始导入第"+pageNum+"页数据-------------");
            Page<Item> page = itemService.lambdaQuery().eq(Item::getStatus, "1").page(new Page<>(pageNum, pageSize));
            if(CollUtil.isEmpty(page.getRecords())){
                break;
            }
            //批量导入到es
            BulkRequest bulkRequest = new BulkRequest();
            page.getRecords().forEach(item -> {
                IndexRequest request = new IndexRequest("item").id(String.valueOf(item.getId()));
                ItemDoc itemDoc = BeanUtil.copyProperties(item, ItemDoc.class);
                request.source(JSONUtil.toJsonStr(itemDoc), XContentType.JSON);
                bulkRequest.add(request);
            });
            client.bulk(bulkRequest, RequestOptions.DEFAULT);
            System.out.println("------------导入第"+pageNum+"页数据完成--------------");
            pageNum++;

        }
    }
    //批量删除
    @Test
    public void test3() throws IOException {
        //分页获取
        int pageNum=1;
        int pageSize=1000;
        while (true){
            System.out.println("----------开始删除第"+pageNum+"页数据-------------");
            Page<Item> page = itemService.lambdaQuery().eq(Item::getStatus, "1").page(new Page<>(pageNum, pageSize));
            if(CollUtil.isEmpty(page.getRecords())){
                break;
            }
            //批量导入到es
            BulkRequest bulkRequest = new BulkRequest();
            page.getRecords().forEach(item -> {
                DeleteRequest request = new DeleteRequest("item").id(String.valueOf(item.getId()));
                bulkRequest.add(request);
            });
            client.bulk(bulkRequest, RequestOptions.DEFAULT);
            System.out.println("------------删除第"+pageNum+"页数据完成--------------");
            pageNum++;

        }
    }
}
